﻿#pragma once
#include <iostream>
#include <vector>
#include <time.h>
#include <assert.h>
#include <thread>
#include <mutex>
using std::cout;
using std::endl;

static const int MAX_BYTES = 256 * 1024;
static const int NFREELIST = 208;


#ifdef _WIN64
	typedef unsigned long long PAGE_ID;
#elif _WIN32
	//typedef unsigned int PAGE_ID;
	typedef size_t PAGE_ID;
#else
	//linux
#endif


static void*& NextObj(void* obj)
{
	return *(void**)obj;
}

// 管理切分好的小对象的自由链表
class FreeList
{
public:
	//头插
	void Push(void* obj)
	{
		assert(obj);
		
		//多次使用，把它接口化
		NextObj(obj) = _freeList;
		//*(void**)obj = _freeList;
		_freeList = obj;
	}

	//头删
	void* Pop()
	{
		assert(_freeList);

		void* obj = _freeList;
		//_freeList = *(void**)obj;
		_freeList = NextObj(obj);
		return obj;
	}

	bool Empty()
	{
		return _freeList == nullptr;
	}

	//引用 可以改变
	size_t& MaxSize()
	{
		return _maxSize;
	}

	void PushRange(void* start, void* end)
	{
		// 也可以是== nullptr 这里本身 就是 nullptr
		NextObj(end) = _freeList;
		_freeList = start;
	}
private:
	void* _freeList = nullptr;
	size_t _maxSize = 1;
};

//计算对象大小的对齐规则
class SizeClass
{
public:
	// 整体控制在最多10%左右的内碎片浪费
	// [1,128]					8byte对齐	    freelist[0,16)
	// [128+1,1024]				16byte对齐	    freelist[16,72)
	// [1024+1,8*1024]			128byte对齐	    freelist[72,128)
	// [8*1024+1,64*1024]		1024byte对齐     freelist[128,184)
	// [64*1024+1,256*1024]		8*1024byte对齐   freelist[184,208)
	//size_t _RoundUp(size_t size, size_t alignNum)
	//{
	//	size_t alignSize;
	//	if (size % alignNum == 0)
	//	{
	//		alignNum = size;
	//	}
	//	else
	//	{
	//		alignNum = (size / alignNum + 1) * alignNum;
	//	}

	//	return alignNum;
	//}

	static inline size_t _RoundUp(size_t bytes, size_t alignNum)
	{
		return (bytes + alignNum - 1) & ~(alignNum - 1);
	}
	
	//取整
	static inline size_t RoundUp(size_t bytes)
	{
		if (bytes < 128)
		{
			return _RoundUp(bytes, 8);
		}
		else if (bytes < 1024)
		{
			return _RoundUp(bytes, 16);
		}
		else if (bytes < 1024 * 8)
		{
			return _RoundUp(bytes, 128);
		}
		else if (bytes < 1024 * 64)
		{
			return _RoundUp(bytes, 1024);
		}
		else if (bytes < 1024 * 258)
		{
			return _RoundUp(bytes, 1024 * 8);
		}
		else
		{
			assert(false);
			return -1;
		}
	}

	//size_t _Index(size_t size, size_t alignNum)
	//{
	//	if (size % alignNum != 0)
	//	{
	//		return size / alignNum + 1;
	//	}
	//	else
	//	{
	//		return size / alignNum;
	//	}
	//}

	static inline size_t _Index(size_t bytes, size_t align_shift)
	{
		return ((bytes + (1 << align_shift) - 1) >> align_shift) - 1;
	}

	// 计算映射的哪一个自由链表桶
	static inline size_t Index(size_t bytes)
	{
		assert(bytes <= MAX_BYTES);

		// 每个区间有多少个链
		static int group_array[4] = { 16, 56, 56, 56 };
		if (bytes < 128)
		{
			return _Index(bytes, 3);
		}
		else if (bytes < 1024)
		{
			return _Index(bytes - 128, 4) + group_array[0];
		}
		else if (bytes < 8 * 1024)
		{
			return _Index(bytes - 1024, 7) + group_array[0] + group_array[1];
		}
		else if (bytes < 64 * 1024)
		{
			return _Index(bytes - 8 * 1024, 10) + group_array[0] + group_array[1] + group_array[2];
		}
		else if (bytes < 128 * 1024)
		{
			return _Index(bytes - 64 * 1024, 13) + group_array[0] + group_array[1] + group_array[2] + group_array[3];
		}
		else
		{
			assert(false);
			return -1;
		}
	}

	// 一次thread cache从中心缓存获取多少个 内存对象
	static size_t NumMoveSize(size_t bytes)
	{
		assert(bytes > 0);

		// [2, 512]，一次批量移动多少个对象的(慢启动)上限值
		// 小对象一次批量上限高
		// 小对象一次批量上限低

		int num = MAX_BYTES / bytes;
		if (num < 2)
			return 2;
		else if (num > 512)
			return 512;

		return num;
	}

};






// Span管理一个 跨度 的大块内存 

// 管理以页为单位的大块内存 
// 管理多个连续页⼤块内存跨度结构 
struct Span
{
	PAGE_ID _page = 0; // 大块内存起始页的页号 ‘
	size_t n = 0; 

	Span* _next = nullptr; //双向链表的结构
	Span* _prev = nullptr;

	size_t _useCount = 0;		// 切好小块内存，被分配给thread cache的计数
	void* _freeList = nullptr; //切好的小块内存的自由链表
};

//双向带头链表

class SpanList
{
public:
	SpanList()
	{
		_head = new Span;
		_head->_next = _head;
		_head->_prev = _head;
	}

	void Insert(Span* pos, Span* newSpan)
	{
		assert(pos);
		assert(newSpan);

		Span* prev = pos->_prev;
		// prev newspan pos

		prev->_next = newSpan;
		newSpan->_prev = prev;
		newSpan->_next = pos;
		pos->_prev = newSpan;
	}

	void Erase(Span* pos)
	{
		assert(pos);
		//双链表 注意判断哨兵位
		assert(pos != _head);
		Span* prev = pos->_prev;

		prev->_next = pos->_next;
		pos->_next->_prev = prev;
	}
private:
	Span* _head;
public:
	std::mutex _lock;
};